package personal.jinhui.paprika.service;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.security.NoSuchAlgorithmException;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.jivesoftware.smack.Connection;

import personal.jinhui.paprika.smack.avatar.AvatarCache;
import personal.jinhui.paprika.smack.avatar.AvatarManager;
import personal.jinhui.paprika.smack.avatar.AvatarMetadataExtension;
import personal.jinhui.paprika.smack.pep.PepSubManager;

/**
 * An AvatarManager for Paprika. It allows to publish avatar on the Android
 * platform.
 */
public class PaprikaAvatarManager extends AvatarManager {
	private static final String TAG = PaprikaAvatarManager.class
			.getSimpleName();
	private static final int JPEG_QUALITY = 100;

	private Context mContext;

	/**
	 * Create a PaprikaAvatarManager.
	 * 
	 * @param ctx
	 *            the Android context
	 * @param con
	 *            the connection
	 * @param pepMgr
	 *            the PepSubManager of the connection
	 * @param cache
	 *            the cache which will store the avatars
	 * @param autoDownload
	 *            tre to enable auto download of avatars
	 */
	public PaprikaAvatarManager(final Context ctx, final Connection con,
			final PepSubManager pepMgr, final AvatarCache cache,
			final boolean autoDownload) {
		super(con, pepMgr, cache, autoDownload);
		mContext = ctx;
	}

	/**
	 * Publish an avatar.
	 * 
	 * @param avatarUri
	 *            the uri of the avatar
	 * @return true if the avatar was successfully published
	 */
	public void publishAvatar(Uri avatarUri) {
		try {
			Bitmap bmp = MediaStore.Images.Media.getBitmap(
					mContext.getContentResolver(), avatarUri);
			try {
				AvatarMetadataExtension meta = new AvatarMetadataExtension();
				byte[] data = getBitmapByte(bmp, Bitmap.CompressFormat.PNG,
						JPEG_QUALITY);
				String dataid = caclAvatarId(data);
				String mimetype = "image/png";
				AvatarMetadataExtension.Info png = new AvatarMetadataExtension.Info(
						dataid, mimetype, data.length);
				png.setHeight(bmp.getHeight());
				png.setWidth(bmp.getWidth());
				meta.addInfo(png);
				publishAvatarMetaData(png.getId(), meta);
				publishAvatarData(data);
			} catch (NoSuchAlgorithmException ex) {
				Log.d(TAG, "Error while publishing avatar " + avatarUri, ex);
			}
		} catch (IOException e) {
			Log.d(TAG, "Error while publishing avatar " + avatarUri, e);
		}
	}

	/**
	 * Convert the bitmap to a byte array.
	 * 
	 * @param bitmap
	 *            the avatar bitmap
	 * @param format
	 *            the resulting image format
	 * @param quality
	 *            the compression quality use for JPEG compression
	 * @return the bitmap data or a array of 0 element on error
	 */
	private byte[] getBitmapByte(Bitmap bitmap, Bitmap.CompressFormat format,
			int quality) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		if (bitmap.compress(format, quality, bos))
			return bos.toByteArray();
		else
			return new byte[0];
	}
}
